package com.msconfig.mvp.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.geo.msconfig.R;
import com.msconfig.bean.dialogBean.EllipsoidParameterBean;
import com.msconfig.event.EllipsoidDialogDismissEvent;
import com.msconfig.mvp.view.adapter.EllipsoidAdapter;

import org.greenrobot.eventbus.EventBus;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

import static com.msconfig.mvp.view.fragment.SetupEllipsoidParameterFragment.RLELLIPSOIDNAME;

public class SetupEllipsoidSelectDialogActivity extends Activity {
    private ArrayList<EllipsoidParameterBean> arrayList;
    private ArrayList<String> ellipsoidNames;
    private EllipsoidParameterBean ellipsoidBean;
    private ListView listView;
    private RelativeLayout rlParent;
    private EllipsoidAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_activity_ellipsoid_select_dialog);
        listView=findViewById(R.id.lv_listview);
        rlParent=findViewById(R.id.rl_parent);
        arrayList=new ArrayList<>();
        ellipsoidNames=new ArrayList<>();
        ArrayList<EllipsoidParameterBean> list=getFromAssets(getString(R.string.file_ellipsoid_csv));
        if(list!=null){
            adapter=new EllipsoidAdapter(this,list);
            listView.setAdapter(adapter);
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setCurrentItem(position);
                adapter.setClick(true);
                //刷新这个才能重新执行getView方法
                adapter.notifyDataSetChanged();
                EllipsoidParameterBean bean= (EllipsoidParameterBean) parent.getItemAtPosition(position);
                //更新参数设置页面
                EventBus.getDefault().post(new EllipsoidDialogDismissEvent(bean));
                finish();
            }
        });

        //选中已选择的
        Intent intent=getIntent();
        String data=intent.getStringExtra(RLELLIPSOIDNAME);
        int index=ellipsoidNames.indexOf(data);
        if(index==-1){
            adapter.setCurrentItem(0);
            adapter.setClick(true);
            listView.setSelection(0);
        }else{
            adapter.setCurrentItem(index);
            adapter.setClick(true);
            listView.setSelection(index);
        }
    }

    //从assets文件中读取椭球信息
    public ArrayList<EllipsoidParameterBean> getFromAssets(String fileName){
        try {
            InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) );
            BufferedReader bufReader = new BufferedReader(inputReader);
            String strLineResult="";
            String[] strings;
            while((strLineResult = bufReader.readLine()) != null){
                ellipsoidBean=new EllipsoidParameterBean();
                strings=strLineResult.split(",");
                if(strLineResult.equals("") && strings.length!=3){
                    continue;
                }
                if(strings[0].length()>0){
                    ellipsoidBean.setName(strings[0]);
                    ellipsoidNames.add(strings[0]);
                }
                if(strings[1].length()>0){
                    ellipsoidBean.setLongHalfshaft(strings[1]);
                }
                if(strings[2].length()>0){
                    ellipsoidBean.setFlatteningInverse(strings[2]);
                }
                arrayList.add(ellipsoidBean);
            }
            return arrayList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
